package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DeptGroupRelDTO;
import com.pshcp.domain.model.DeptGroupRel;
import com.pshcp.mapper.DeptGroupRelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科室分组关联表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2023-02-07
 */
@Service
public class DeptGroupRelService {

    @Autowired
    private DeptGroupRelMapper deptGroupRelMapper;

    public PageList<DeptGroupRel> list(DeptGroupRelDTO deptGroupRelDTO){
        if(deptGroupRelDTO.getPageNum() != null && deptGroupRelDTO.getPageSize() != null){
            PageHelper.startPage(deptGroupRelDTO.getPageNum(), deptGroupRelDTO.getPageSize());
        }
        Example example = new Example(DeptGroupRel.class);
        Example.Criteria criteria = example.createCriteria();
        if(deptGroupRelDTO.getStatus() != null){
            criteria.andEqualTo("status", deptGroupRelDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<DeptGroupRel> list = deptGroupRelMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DeptGroupRel> select(DeptGroupRel deptGroupRel){
        return deptGroupRelMapper.select(deptGroupRel);
    }

    public int insert(DeptGroupRel deptGroupRel){
        deptGroupRel.setId(SystemUtil.getId());
        deptGroupRel.setStatus(CommonStatusEnum.INIT.getCode());
        deptGroupRel.setCreatedTime(new Date());
        return deptGroupRelMapper.insert(deptGroupRel);
    }

    public int update(DeptGroupRel deptGroupRel){
        deptGroupRel.setUpdateTime(new Date());
        return deptGroupRelMapper.updateByPrimaryKeySelective(deptGroupRel);
    }

    public DeptGroupRel findById(Long id){
        return deptGroupRelMapper.selectByPrimaryKey(id);
    }

    public List<DeptGroupRel> findByIds(List<Long> ids){
        Example example = new Example(DeptGroupRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return deptGroupRelMapper.selectByExample(example);
    }

    public List<DeptGroupRel> findByDeptId(Long deptId){
        Example example = new Example(DeptGroupRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deptId", deptId);
        return deptGroupRelMapper.selectByExample(example);
    }

    public List<DeptGroupRel> findByGroupId(Long groupId){
        Example example = new Example(DeptGroupRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId", groupId);
        return deptGroupRelMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(DeptGroupRel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DeptGroupRel deptGroupRel = new DeptGroupRel();
        deptGroupRel.setStatus(CommonStatusEnum.DEL.getCode());
        return deptGroupRelMapper.updateByExampleSelective(deptGroupRel, example);
    }

}
